﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WorkFlowBLL;
using System.Text.RegularExpressions;

namespace QLCF.Forms
{
    public partial class frmProductDetail : Form
    {
        private Boolean isAddData = false;
        public frmProduct m_parrent;
        public int m_productID = -1;
        public int m_categoryID = -1;
        
        public frmProductDetail(frmProduct Parent)
        {
            InitializeComponent();         
            this.m_productID = Parent.m_productID;
            this.isAddData = Parent.isAddData;            
            this.m_parrent = Parent;
            this.m_categoryID = Parent.m_categoryID;        
        }

        private void frmProductDetail_Load(object sender, EventArgs e)
        {            
            BindCbxStatus();
            GetListCategory();
            GetListUnit();
            if (!isAddData)
            {
                CtrProduct ctrProduct = new CtrProduct();
                var objProduct = ctrProduct.ProductGetInfo(this.m_productID);
                if (objProduct != null)
                {
                    txtNameProduct.Text = objProduct.ProductName;
                    txtPrice.Text = objProduct.Price.ToString();                    
                    txtDescription.Text = objProduct.Description;
                    cbxCategory.SelectedValue = objProduct.CategoryID;
                    cbxStatus.SelectedValue = objProduct.Status;
                    cbxUnit.SelectedValue = objProduct.UnitID;
                        
                    if (objProduct.IsQuantification == true)
                    {
                        cbxIsQuantification.Checked = true;
                    }
                    else
                    {
                        cbxIsQuantification.Checked =  false;
                    }
                    
                    if (objProduct.IsHasStock == true)
                    {
                        cbxExistStock.Checked = true;
                    }
                    else
                    {
                        cbxExistStock.Checked = false;
                    }
                }
            }
        }        

        private void BindCbxStatus()
        {
            cbxStatus.Items.Clear();
            cbxStatus.DisplayMember = "text";
            cbxStatus.ValueMember = "value";
            List<EntityBLL.ListItem> lst1 = new List<EntityBLL.ListItem>();
            lst1.Add(new EntityBLL.ListItem(1, "Hoạt động"));
            lst1.Add(new EntityBLL.ListItem(0, "Khóa"));
            cbxStatus.DataSource = lst1;
            cbxStatus.SelectedValue = 1;
        }

        private void GetListUnit()
        {
            CtrBase ctrBase = new CtrBase();
            var objBase = ctrBase.GetUnitByBaseUnitID(0);
            cbxUnit.DataSource = objBase;
            cbxUnit.DisplayMember = "Name";
            cbxUnit.ValueMember = "UnitID";
            cbxUnit.SelectedIndex = 0;
        }

        private void GetListCategory()
        {
            CtrCategory ctrCategory = new CtrCategory();
            if (!this.isAddData)
            {
                var objCategory = ctrCategory.GetListCategory(-1);
                cbxCategory.DisplayMember = "CategoryName";
                cbxCategory.ValueMember = "CategoryID";
                cbxCategory.DataSource = objCategory;   
            }
            else
            {
                var objCategory = ctrCategory.GetListCategory();
                cbxCategory.DisplayMember = "CategoryName";
                cbxCategory.ValueMember = "CategoryID";
                cbxCategory.DataSource = objCategory;   
            }                     
            if (this.m_categoryID == -1)
            {
                cbxCategory.SelectedIndex = 0;
            }
            else
            {
                cbxCategory.SelectedValue = this.m_categoryID;
            }            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool CheckData()
        {
            if (this.txtNameProduct.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập tên mặt hàng", "Thông báo...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtNameProduct.Focus();
                return false;
            }            
            if (this.txtPrice.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập giá mặt hàng", "Thông báo...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtPrice.Focus();
                return false;
            }
            if (!IsNumber(this.txtPrice.Text))
            {
                MessageBox.Show("Giá mặt hàng phải là số nguyên > 0", "Thông báo...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtPrice.Focus();
                return false;
            }

            if (Convert.ToInt32(this.txtPrice.Text) <= 0)
            {
                MessageBox.Show("Giá mặt hàng phải là số nguyên > 0", "Thông báo...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtPrice.Focus();
                return false;
            }
            if (Convert.ToInt32(this.txtPrice.Text) > 1000000000)
            {
                MessageBox.Show("Giá mặt hàng phải là số nguyên <100000000", "Thông báo...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtPrice.Focus();
                return false;
            }
            return true;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (!this.CheckData()) return;
            int isQuantification = 0;
            int isExistStock = 0;
            if (cbxIsQuantification.Checked) isQuantification = 1;
            if (cbxExistStock.Checked) isExistStock = 1;            
             
            if (isAddData)
            {
                CtrProduct ctrProduct = new CtrProduct();
                var objProduct = ctrProduct.ProductInsert(txtNameProduct.Text, Convert.ToInt32(cbxUnit.SelectedValue), cbxUnit.Text, Convert.ToInt32(txtPrice.Text), Convert.ToInt32(cbxCategory.SelectedValue), txtDescription.Text,isQuantification == 1 ? true:false,isExistStock==1? true:false, Convert.ToInt32(cbxStatus.SelectedValue));
                if (objProduct > 0)
                {
                    CtrAdmin ctra = new CtrAdmin();
                    ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 1, "Thêm mới sản phẩm \"" + txtNameProduct.Text + "\" có mã \"" + objProduct + "\"", "Quản lý mặt hàng");

                    m_parrent.bindData(this.m_categoryID);
                    this.Close();
                }
            }
            else
            {
                CtrProduct ctrProduct = new CtrProduct();
                var objProduct = ctrProduct.ProductUpdate(this.m_productID, txtNameProduct.Text, Convert.ToInt32(cbxUnit.SelectedValue), cbxUnit.Text, Convert.ToInt32(txtPrice.Text), Convert.ToInt32(cbxCategory.SelectedValue), txtDescription.Text, isQuantification == 1 ? true : false, isExistStock == 1 ? true : false, Convert.ToInt32(cbxStatus.SelectedValue));
                if (objProduct > 0)
                {
                    CtrAdmin ctra = new CtrAdmin();
                    ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 2, "Cập nhật sản phẩm \"" + txtNameProduct.Text + "\" có mã \"" + this.m_productID + "\"", "Quản lý mặt hàng");

                    m_parrent.bindData(this.m_categoryID);
                    this.Close();
                }
            }
        }

        private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
                
        bool IsNumber(string text)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(text);
        }
    }
}
